const { Controller } = require('egg');

class UserController extends Controller {
  /**
   * 用户登录
   */
  async login() {
    const { ctx, service } = this;
    const { username, password } = ctx.request.body;
    
    // 验证参数
    if (!username || !password) {
      ctx.status = 400;
      ctx.body = {
        success: false,
        message: '用户名和密码不能为空',
      };
      return;
    }
    
    // 调用服务验证登录
    const loginResult = await service.user.login(username, password);
    
    if (loginResult.success) {
      ctx.status = 200;
      ctx.body = {
        success: true,
        data: {
          token: loginResult.token,
          username: loginResult.username,
        },
        message: '登录成功',
      };
    } else {
      ctx.status = 200;
      ctx.body = {
        success: false,
        message: loginResult.message || '登录失败',
      };
    }
  }
  
  /**
   * 验证当前登录状态
   */
  async checkLoginStatus() {
    const { ctx } = this;
    
    ctx.body = {
      success: true,
      data: {
        isLoggedIn: true,
        username: ctx.user.username,
      },
    };
  }
}

module.exports = UserController; 